package com.cjl.web.backend;

import com.cjl.entity.EbNews;
import com.cjl.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller //标记为服务员
@RequestMapping( "/news" ) //工号
public class NewsController {
    @Autowired //spring 注入 给newsService赋值
    private NewsService newsService;

    /**
     * @RequestParam("page") page必须与请求参数的名字一直
     * spring mvc自动将请求参数的值绑定list方法的psize参数
     * ?page=2 ==spring mvc==》 psize=2
     * @RequestParam(value = "page" , defaultValue = "1")
     * defaultValue=1表示如果请求没有携带page参数，则psize的默认值为1
     */
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "page", defaultValue = "1") int psize,
                       @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {//alt+enter) {//alt+enter
        System.out.println("start service.....;psize==>" + psize);
        //1.调用Service层的类的方法获取分页的新闻数据
        List<EbNews> newsList = newsService.listByPage(psize, 3);
        //2.调用Service层的类的方法获取总页数
        final Integer totalPage = newsService.findTotalPage(pageSize);
        //3.将model和jsp整合   kely-value键值对
        //3.1 model中绑定分页的新闻数据
        model.addAttribute("newsList", newsList);
        //3.2 mode l中绑定总页数
        model.addAttribute("totalPage", totalPage);
        //3.3 model中绑定当前页
        model.addAttribute("currPage", psize);
        //3.4 jsp
        return "manage/news";//manage/news.jsp


    }

    @RequestMapping("/to_save")
    public String toSave() {
        // spingmvc框架:前缀/WEB—INF/ manage/news-add.jsp后缀
        return "manage/news-add";
    }

    @RequestMapping("/save")
    public String save(EbNews ebNews) {
        System.out.println("ebNews==>" + ebNews);
        //l1.调用service层的添加新闻的方法
        //result 返回时受影响的行数
        Integer result = newsService.save(ebNews);
        System.out.println("result:" + result);
        //2.根据result 的结果>0?决定跳转网页
        if (result <= 0) {
            //TODO 添加失败后的处理1、提示失败2.抛出异常
            return "manage/manage-error";
        }
        return "manage/manage-result";//manage/manage-result.jsp
    }


    @RequestMapping("/to_edit")
    public String toEdit(Model model,
                         @RequestParam("enId") int enId) {
        System.out.println("点击修改跳过来了,enId=" + enId);
        //1.调用service层的方法查询点击的新闻的所有信息
        EbNews ebNews = newsService.selectOne(enId);
        //2.指定model和jsp
        model.addAttribute("ebNews", ebNews);
        return "manage/news-modify";//manage/news-modify.jsp
    }

    @RequestMapping("/edit")
    public String edit(EbNews ebNews) {
        System.out.println("ebNews==>" + ebNews);
        //1.调用service层的更新新闻的方法
        Integer result = newsService.updateNews(ebNews);
        //2.根据更新返回过来的结果决定跳转页面
        if (result < 1) {
            //更新失败的情况
            return "manage/manage-error";
        }
        //更新成功的情况
        return "manage/manage-result";
    }

    @RequestMapping("/delete")
    public String delete(Integer enId){
        //1.参数校验

        //2.执行删除（真实项目中 delete from 表名 where id=?）
        int result =newsService.deleteByPk(enId);
        if(result==0){
            //TODO 要么return错误页面 要么跑出异常
        }
        return "manage/manage-result";
    }
}